<?php
declare (strict_types = 1);

namespace app\admin\validate;

use think\Validate;

class User extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
        "account|用户名" => "require",
        "parent_code|推荐人编号" => "require",
        "mobile|手机号" => "require|mobile|checkUnique",
    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        //'code.require' => '请填写会员编号',
//        'code.unique' => '会员编号已被注册',
        'parent_code.require' => '请填写推荐人编号',
        'nickname.require' => '请填写昵称',
//        'nickname.unique' => '用户名已被注册',
        'mobile.require' => '请填写手机号',
        'mobile.mobile' => '手机号格式错误',
        'mobile.unique' => '手机号已被注册',
    ];

    // 自定义验证规则
    protected function checkUnique($value, $rule, $data=[], $field)
    {
        $where = [['is_del', '=', 0],[$field,'=',$value]];
        array_key_exists('id',$data) ? $where[] = ['id','<>',$data['id']] : '';
        $count = \app\common\model\User::where($where)->count();
        if($count){
            return false;
        }
        return true;
    }

    //protected $message = [
    //    'mobile.checkUnique' =>   '手机号已存在',
    //];
}
